package com.lk.client.rpc.handler;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.timeout.IdleStateHandler;

import java.util.concurrent.TimeUnit;

/**
 * @author kun.li
 */
public class InitializerHandler extends ChannelInitializer<NioSocketChannel> {
    MessageCodec messageCodec = new MessageCodec();
    RpcResponseMessageHandler responseMessageHandler=new RpcResponseMessageHandler();

    @Override
    protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
        ChannelPipeline pipeline = nioSocketChannel.pipeline();
        HeartBeatClientHandler HEATBEAT_CLIENT = new HeartBeatClientHandler();
        // 增加handler链
        pipeline.addLast(new IdleStateHandler(30,0,0, TimeUnit.SECONDS));
        // 用来处理黏包和半包问题
        pipeline.addLast(new ProcotolFrameDecoder());
        // 编解码handler
        pipeline.addLast(messageCodec);
        pipeline.addLast(HEATBEAT_CLIENT);
        // 自定义handler
        pipeline.addLast(responseMessageHandler);

    }
}
